const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();

function min(v1, v2) {
  let var1 = v1;
  let var2 = v2;
  var1 = parseFloat(var1);
  var2 = parseFloat(var2);
  return var1 > var2 ? var2 : var1;
}

Page({
  data: {
    price: 0.00,
    cash_max_day: -1,
    selected: -1,
    unit: '',
    cash_all: '',
    height: wx.systemInfo.windowHeight,
    typeId: 1,
    pay_type_new: [],
    imgUrl: app.imgUrl,
  },
  onLoad(options) {
    let that = this;
    let type = options.type ? Number(options.type) : 1;
    let typeId = options.type_id ? Number(options.type_id) : null;

    let isMulti = app.ws_micro_shop_globalData && (app.ws_micro_shop_globalData.isMulti || app.ws_micro_shop_globalData.isBrand);  // 是否为多店

    that.setData({ type, typeId, isMulti });
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
  },
  onShow() {
    let share_setting = wx.getStorageSync('share_setting');
    this.setData({
      share_setting,
    });
    this.getCashData(); 
  },
  cashAll() {
    this.setData({
      cash_all: this.data.price,
    })
  },
  getInputNum(e) {
    this.setData({
      cash_all: e.detail.value,
    })
  },
  getInputValue(e) {
    let { value } = e.detail;
    let { name } = e.currentTarget.dataset;
    this.setData({
      [name]: value,
    })
  },
  // 获取提现数据
  getCashData() {
    let that = this;
    let { typeId, isMulti } = that.data;
    let updataUrl = api.share.get_price;
    updataUrl = typeId ? (Number(typeId) === 1 ? api.adminer.withdraw : api.user.withdraw) : updataUrl;
    // updataUrl = (Number(typeId) === 1) ? api.adminer.withdraw : updataUrl;
    app.ws_micro_shoprequest({
      url: updataUrl,
      method: 'GET',
      success: function (res) {
        if (typeId) {
          // 商家提现
          if (res.code === 0) {
            let pay_type_copy = [];
            // let typeList =  (typeof res.data.type_list === 'string') ? JSON.parse(res.data.type_list) : res.data.type_list;
            // for (let i = 0; i < typeList.length; i++) {
            //   pay_type_copy.push(typeList[i].id);
            // }
            let pay_type_new = (typeof res.data.type_list === 'string') ? JSON.parse(res.data.type_list) : res.data.type_list;
            // if (isMulti) pay_type_new.push({ "id": 1, "name": "微信线下转账"})
            that.setData({
              // selected: 1,
              name: '',
              mobile: '',
              bank_name: '',
              price: res.data.account_money || res.data.money ? (Number(typeId) === 1 ? res.data.account_money : res.data.money) : '0.00',
              pay_type_copy,
              pay_type_new,
              payment: pay_type_new[0].name,
              selected: pay_type_new[0].id,
            });
          }
        } else {
          // 分销提现
          if (res.code === 0) {
            let selected = 0;
            let name = '';
            let mobile = '';
            let bank_name = '';
            let { cash_last, pay_type, pay_type_copy, bank, cash_max_day, price, pay_type_new } = res.data;
            if (cash_last) {
              selected = cash_last['type'];
              name = cash_last['name'];
              mobile = cash_last['mobile'];
              bank_name = cash_last['bank_name'];
            }
            selected = pay_type_new[0].type;
            let lastSelected = cash_last ? Number(cash_last['type']) : 0;
            that.setData({
              price: price ? price.price : 0,
              cash_max_day,
              pay_type,
              lastSelected,
              name,
              mobile,
              bank,
              bank_name,
              name1: selected === lastSelected ? name : '',
              mobile1: selected === lastSelected ? mobile : '',
              bank_name1: selected === lastSelected ? bank_name : '',
              is_bank: bank,
              pay_type_copy,
              pay_type_new,
              payment: pay_type_new[0].name,
              selected,
            });
          }
        }
      },
    });
  },
  pickPayment(e) {
    let index = Number(e.detail.value);
    let { pay_type_new, typeId } = this.data;
    this.setData({
      payment: pay_type_new[index].name,
      selected: typeId ? pay_type_new[index].id : pay_type_new[index].type,
      name1: '',
      mobile1: '',
      bank_name1: '',
    })
  },
  //分销微信线上打款
  submit() {
    let { price: cash_max, selected, cash_max_day, unit, share_setting, cash_all } = this.data;
    cash_all = parseFloat(parseFloat(cash_all).toFixed(2));
    Number(cash_max_day) !== -1 && (cash_max = min(cash_max, cash_max_day));
    if (cash_all > cash_max) {
      wx.showToast({
        title: '提现金额不能超过' + unit + cash_max,
        icon: 'none',
      });
      return;
    }
    if (cash_all < parseFloat(share_setting.min_money)) {
      wx.showToast({
        title: '提现金额不能低于' + unit + share_setting.min_money,
        icon: 'none',
      });
      return;
    }
    wx.showLoading({
      title: '正在提交',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.share.apply,
      method: 'POST',
      data: {
        cash: cash_all,
        scene: 'CASH',
        pay_type: 5, // 微信提现  等于0
      },
      success: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: res.msg,
          icon: 'none',
        })
        if (res.code === 0) {
          setTimeout(()=> {
            wx.redirectTo({
              url: '/ws_micro_shop_seller/pages/share-detail/share-detail?price=' + cash_all + '&payment=微信钱包',
            });
          }, 1500);
        }

        
        // wx.showModal({
        //   title: '提示',
        //   content: res.msg,
        //   showCancel: false,
        //   success: function (e) {
        //     if (e.confirm) {
        //       if (res.code === 0) {
        //         wx.redirectTo({
        //           url: '/ws_micro_shop_seller/pages/share-detail/share-detail?price=' + cash_all + '&payment=微信钱包',
        //         });
        //       }
        //     }
        //   },
        // });
      },
    });
  },
  // 分销提现提交
  formSubmit(e) {
    let { cash, name, mobile, bank_name } = e.detail.value;
    let { price: cash_max, selected, cash_max_day, unit, share_setting, payment } = this.data;
    cash = parseFloat(parseFloat(cash).toFixed(2));
    selected = Number(selected);
    Number(cash_max_day) !== -1 && (cash_max = min(cash_max, cash_max_day));
    if (cash > cash_max) {
      wx.showToast({
        title: '提现金额不能超过' + unit + cash_max,
        icon: 'none',
      });
      return;
    }
    if (cash < parseFloat(share_setting.min_money)) {
      wx.showToast({
        title: '提现金额不能低于' + unit + share_setting.min_money,
        icon: 'none',
      });
      return;
    }
    if (!name) {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (!mobile) {
      wx.showToast({
        title: '账号不能为空',
        icon: 'none',
      });
      return;
    }
    if (selected !== 0 && selected !== 1 && selected !== 3) {
      wx.showToast({
        title: '请选择提现方式',
        icon: 'none',
      });
      return;
    }
    if (!bank_name) {
      bank_name = '';
    }
    if (selected === 3) {
      if (!bank_name) {
        wx.showToast({
          title: '开户行不能为空',
          icon: 'none',
        });
        return;
      }
    }
    wx.showLoading({
      title: '正在提交',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.share.apply,
      method: 'POST',
      data: {
        cash,
        name,
        mobile,
        bank: bank_name,
        pay_type: selected,
        scene: 'CASH',
        form_id: e.detail.formId,
      },
      success: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: res.msg,
          icon: 'none',
        })
        if (res.code === 0) {
          setTimeout(()=> {
            wx.redirectTo({
              url: '/ws_micro_shop_seller/pages/share-detail/share-detail?price=' + cash + '&payment=' + payment,
            });
          }, 1500);
        }
        // wx.showModal({
        //   title: '提示',
        //   content: res.msg,
        //   showCancel: false,
        //   success: function (e) {
        //     if (e.confirm) {
        //       if (res.code === 0) {
        //         wx.redirectTo({
        //           url: '/ws_micro_shop_seller/pages/share-detail/share-detail?price=' + cash + '&payment=' + payment,
        //         });
        //       }
        //     }
        //   },
        // });
      },
    });
  },
  // 商家提现提交
  formBusinessSubmit(e) {
    let { cash, name, mobile, bank_name } = e.detail.value;
    let { price: cash_max, selected, typeId, isMulti, payment } = this.data;
    cash = parseFloat(parseFloat(cash).toFixed(2));
    selected = Number(selected);
    if (!cash) {
      wx.showToast({
        title: '提现金额不能为空',
        icon: 'none',
      });
      return;
    }
    if (cash > cash_max) {
      wx.showToast({
        title: '提现金额不能超过￥' + cash_max,
        icon: 'none',
      });
      return;
    }
    if (cash < 1) {
      wx.showToast({
        title: '提现金额不能低于￥1.00',
        icon: 'none',
      });
      return;
    }
    if (selected === 5) {
      if (selected !== 1 && selected !== 2 && selected !== 3 && selected !== 5) {
        wx.showToast({
          title: '请选择提现方式',
          icon: 'none',
        });
        return;
      } 
    } else {
      if (selected !== 1 && selected !== 2 && selected !== 3) {
        wx.showToast({
          title: '请选择提现方式',
          icon: 'none',
        });
        return;
      } else {
        if (!name) {
          wx.showToast({
            title: '姓名不能为空',
            icon: 'none',
          });
          return;
        }
        if (!mobile) {
          wx.showToast({
            title: '账号不能为空',
            icon: 'none',
          });
          return;
        }
        if (!bank_name) {
          bank_name = '';
        }
        if (selected === 3) {
          if (!bank_name) {
            wx.showToast({
              title: '开户行不能为空',
              icon: 'none',
            });
            return;
          }
        }
      }
    }
    wx.showLoading({
      title: '正在提交',
      mask: true,
    });
    let cashData = {
      account: mobile,
      nickname: name,
    };
    let updata = {
      money: cash,
      type: selected,
      'data[account]': mobile,
      'data[nickname]': name,
      'data[bank_name]': bank_name || '',
    };
    // updata.data = JSON.stringify(cashData);
    if (selected === 3) {
      // updata.bank_name = bank_name;
    }
    app.ws_micro_shoprequest({
      url: Number(typeId) === 1 ? api.adminer.withdraw : api.user.withdraw,
      method: 'POST',
      data: updata,
      success: function (res) {
        wx.hideLoading();
        wx.showToast({
          title: res.msg,
          icon: 'none',
        })
        if (res.code === 0) {
          setTimeout(()=> {
            wx.redirectTo({
              url: '/ws_micro_shop_seller/pages/share-detail/share-detail?price=' + cash + '&payment=' + payment + '&type_id=' + typeId,
            });
          }, 1500);
        }
        // wx.showModal({
        //   title: '提示',
        //   content: res.msg,
        //   showCancel: false,
        //   success: function (e) {
        //     console.log(e);
        //     if (e.confirm) {
        //       if (res.code === 0) {
        //         wx.redirectTo({
        //           url: `/ws_micro_shop_seller/pages/cash-detail/cash-detail?type_id=${typeId} `,
        //         });
        //       }
        //     }
        //   },
        // });
      },
    });
  },
  showCashMaxDetail() {
    wx.showModal({
      title: '提示',
      content: '今日剩余提现金额=可提现金额-已提现金额',
    });
  },
  select(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      selected: index,
    });
  },
});
